(function($){
	// var $=window.Dom7;
	var FootballDate=function(date,pattern){
		//思路：注入Date对象实现各项功能
		//获取本周几的日期对象
		//获取指定时间的footballDate
		var _date=null;
		var SIGN_REGEXP = /([yMdhsm])(\1*)/g;
		var DEFAULT_PATTERN = 'yyyyMMdd';
		var todayLine;
		var useFootballTimeline=false;//是否启用足球时间，以11:00am为一日的界线
		var _padding=function(s,len){
			var len =len - (s+'').length;
			for(var i=0;i<len;i++){s = '0'+ s;}
			return s;
		}
		var format=function(pattern){
			pattern = pattern||DEFAULT_PATTERN;
			return pattern.replace(SIGN_REGEXP,function($0){
				switch($0.charAt(0)){
					case 'y' : return _padding(_date.getFullYear(),$0.length);
					case 'M' : return _padding(_date.getMonth()+1,$0.length);
					case 'd' : return _padding(_date.getDate(),$0.length);
					case 'w' : return _date.getDay()+1;
					case 'h' : return _padding(_date.getHours(),$0.length);
					case 'm' : return _padding(_date.getMinutes(),$0.length);
					case 's' : return _padding(_date.getSeconds(),$0.length);
				}
			});
		}
		//按照pattern的格式将日期字符串dateString转化为Date对象
		//不支持20151206这样年月日之间没有分割的情况
		var parse=function(dateString,pattern){
			var matchs1=pattern.match(SIGN_REGEXP);
			var matchs2=dateString.match(/(\d)+/g);
			if(matchs1.length==matchs2.length){
				var date = new Date(1970,0,1);
				for(var i=0;i<matchs1.length;i++){
					var _int = parseInt(matchs2[i]);
					var sign = matchs1[i];
					switch(sign.charAt(0)){
						case 'y' : date.setFullYear(_int);break;
						case 'M' : date.setMonth(_int-1);break;
						case 'd' : date.setDate(_int);break;
						case 'h' : date.setHours(_int);break;
						case 'm' : date.setMinutes(_int);break;
						case 's' : date.setSeconds(_int);break;
					}
				}
				return date;
			}
			return null;
		}
		var getDay=function(){
			return _date.getDay();
		}
		var getDayCN=function(){
			var day=_date.getDay();
			switch(day){
				case 0:return '日';
				case 1:return '一';
				case 2:return '二';
				case 3:return '三';
				case 4:return '四';
				case 5:return '五';
				case 6:return '六';
			}
		}
		//获取相对当前_date偏移delta天的FootballDate对象
		var getDeviationDate=function(delta){
			return new FootballDate(new Date(_date.valueOf()+delta*24*60*60*1000));
		}
		//获取本周第day天的FootballDate对象
		var getWeekDay=function(day){
			var delta=day-_date.getDay();
			return getDeviationDate(delta);
		}
		var getDateObject=function(){
			return _date;
		}
		var init=function(date,pattern){
			todayLine=new Date();
			todayLine.setHours(11),todayLine.setMinutes(0),todayLine.setSeconds(0),todayLine.setMilliseconds(0);
			if(!date){
				var now=new Date();
				if(useFootballTimeline&&now<todayLine){
					_date=new Date(now.valueOf()-1*24*60*60*1000);
				}
				else{
					_date=now;
				}
			}
			else if(date&&!pattern){
				if(typeof(date)=='string'){
					_date=parse(date,'yyyy/MM/dd');
				}
				else{
					_date=date;
				}
			}
			else{
				_date=parse(date,pattern);
			}
		}
		init(date,pattern);
		//暴露接口
		return {
			getDay:getDay,
			getDayCN:getDayCN,
			getDeviationDate:getDeviationDate,
			getWeekDay:getWeekDay,
			getDateObject:getDateObject,
			format:format
		}
	};

	var templateHelper={
		init:function(){
			// console.log(window.app);
			// $('body').append(globalTpl);
			
			//注册模板helper方法
			this.registerHelper();
		},
		loadTpl:function(tplId){
			return $('#'+tplId).html();
		},
		renderTplById:function(tplId,renderData){
			var markup=this.loadTpl(tplId);
			var compiledTemplate=Template7.compile(markup);
			var output=compiledTemplate(renderData);
			return output;
		},
		registerHelper:function(){
			Template7.registerHelper('formatDrawDate', function (date){
				var reg=/^(\d{4})(\d{2})(\d{2})$/g;
				var dateWithSeparator=date.replace(reg,function($0,$1,$2,$3){
					return ($1+'/'+$2+'/'+$3);
				});
				var dateObj=new FootballDate(dateWithSeparator,'yyyy/MM/dd');
				return dateObj.format('yyyy年MM月dd日')+' 周'+dateObj.getDayCN();
			});


			Template7.registerHelper('formatCloseTime', function (datetime){
				var dateObj=new FootballDate(datetime,'yyyy-MM-dd hh:mm:ss');
				return dateObj.format('hh:mm');
			});

			Template7.registerHelper('formatHandicap', function (handicap){
				return parseInt(handicap)>0 ? '+'+handicap : handicap;
			});

			Template7.registerHelper('formatOdds', function (odds){
				return parseFloat(odds).toFixed(2);
			});
			Template7.registerHelper('orderStatus', function (orderStatusId){
				//1待支付 2 支付中 3 待出票 4 待开奖 5 待派奖 6 已派奖 7 未中奖 8 已撤单 9 已过期
				var statusMapping=[
					"未知状态",
					"待支付",
					"支付中",
					"待出票",
					"待开奖",
					"待派奖",
					"已派奖",
					"未中奖",
					"已撤单",
					"已过期",
					"撤单返款",
				];
				if(orderStatusId>10 || orderStatusId<0){
					orderStatusId=0;
				}
				return statusMapping[orderStatusId];
			});

			Template7.registerHelper('orderStatusClass', function (orderStatusId){
				//1待支付 2 支付中 3 待出票 4 待开奖 5 待派奖 6 已派奖 7 未中奖 8 已撤单 9 已过期
				var statusMapping=[
					"pending",
					"pending",
					"pending",
					"pending",
					"active",
					"won",
					"won",
					"pending",
					"pending",
					"pending",
					"pending",
				];
				if(orderStatusId>10 || orderStatusId<0){
					orderStatusId=0;
				}
				return statusMapping[orderStatusId];
			});

			Template7.registerHelper('lotteryName', function (lotteryId){
				//1待支付 2 支付中 3 待出票 4 待开奖 5 待派奖 6 已派奖 7 未中奖 8 已撤单 9 已过期
				var statusMapping={
					"0": "未知彩种",
					"46": "竞彩足球"
				};
				return statusMapping[lotteryId] || statusMapping[0];
			});
			Template7.registerHelper('prizeFormat', function (amount){
				return parseFloat(amount)>0? "￥"+amount : "";
			});



		}
		
	};
	templateHelper.init();
	window.TplHelper=templateHelper;
	window.FootballDate=FootballDate;
	// return templateHelper;
})($);